import Taro from '@tarojs/taro-rn';
import * as tslib_1 from "tslib";
import React from 'react';
import { Component } from "@tarojs/taro-rn";
import { View, Image, Text, ScrollView } from "@tarojs/components-rn";
import { toJS } from 'mobx';
import { inject, observer } from "@tarojs/mobx-rn";
import { getRuntime, pageSize, getAvatar, getSimpleTime, partOf, typeOfJob } from "../../utils/common";
import issueStyleSheet from "./issue_styles";
var _styleSheet = issueStyleSheet;

function _getClassName() {
  var className = [];
  var args = arguments[0];
  var type = Object.prototype.toString.call(args).slice(8, -1).toLowerCase();

  if (type === 'string') {
    args = args.trim();
    args && className.push(args);
  } else if (type === 'array') {
    args.forEach(function (cls) {
      cls = _getClassName(cls).trim();
      cls && className.push(cls);
    });
  } else if (type === 'object') {
    for (var k in args) {
      k = k.trim();

      if (k && args.hasOwnProperty(k) && args[k]) {
        className.push(k);
      }
    }
  }

  return className.join(' ').trim();
}

function _getStyle(classNameExpression) {
  var className = _getClassName(classNameExpression);

  var classNameArr = className.split(/\s+/);
  var style = [];

  if (classNameArr.length === 1) {
    style.push(_styleSheet[classNameArr[0].trim()]);
  } else {
    classNameArr.forEach(function (cls) {
      style.push(_styleSheet[cls.trim()]);
    });
  }

  return style;
}

let Personnel = class Personnel extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentNavId: null,
      pageIndex: 1,
      pageSize: pageSize,
      items: [],
      totalCount: 0,
      navArr: [{ id: 1, name: '需求' }, { id: 2, name: '服务' }, { id: 4, name: '人才' }, { id: 3, name: '招聘' }]
    };
  }
  componentWillMount() {
    Taro.setNavigationBarTitle({ title: '我的发布' });
    this.getMyRelease();
  }
  getMyRelease() {
    let { userStore } = this.props.stores;
    let { currentNavId, pageIndex, pageSize } = this.state;
    userStore.getMyRelease({
      publicationType: currentNavId,
      pageIndex,
      pageSize,
      callback: home => {
        // console.log(home)
        if (pageIndex === 1) {
          this.setState({
            totalCount: toJS(home.totalCount),
            items: toJS(home.items)
          }, () => {
            // 职位详情页使用
            getRuntime().setStorage('positionInfo', this.state.items);
          });
        } else {
          const { items } = this.state;
          items.push(...(home.items || []));
          this.setState({
            totalCount: home.totalCount,
            items
          }, () => {
            // 职位详情页使用
            getRuntime().setStorage('positionInfo', this.state.items);
          });
        }
      }
    });
  }
  onReachBottom() {
    let { totalCount, pageSize, pageIndex } = this.state;
    const { loading } = this.props.stores.homeStore;
    if (totalCount > pageIndex * pageSize && !loading) {
      this.setState({
        pageIndex: pageIndex + 1
      }, () => {
        this.getMyRelease();
      });
    }
  }
  handelRouter(url = '/pages/index/index') {
    getRuntime().navigateTo(url);
  }
  changeNavId(id = null) {
    this.setState({
      currentNavId: id,
      items: [],
      pageIndex: 1
    }, () => {
      this.getMyRelease();
    });
  }
  link(url) {
    window.location.href = url;
  }
  // 删除按钮
  delItem(item) {}
  render() {
    let { currentNavId, navArr, items } = this.state;
    return <View style={_styleSheet["newest-index"]}>
        <ScrollView scrollX={true} style={_styleSheet["scroll-view-nav"]}>
          <View onClick={this.changeNavId.bind(this, null)} style={_getStyle(!currentNavId ? 'scroll-item scroll-item-on' : 'scroll-item')}><Text style={_styleSheet["line"]} />全部</View>
          {navArr.map(item => {
          return <View onClick={this.changeNavId.bind(this, item.id)} style={_getStyle(item.id === currentNavId ? 'scroll-item scroll-item-on' : 'scroll-item')}><Text style={_styleSheet["line"]} />{item.name}</View>;
        })}
        </ScrollView>

        {items.length > 0 && items.map(item => {
        let tpl = null;
        switch (item.publicationType) {
          case 1:
            {
              tpl = <View style={_styleSheet["list-11-item"]}>
                  <View onClick={this.handelRouter.bind(this, '/pages/newest/demand_detail?id=' + item.id)} style={_styleSheet["line"]}>
                    <View style={_styleSheet["l"]}>
                      <Image src={getAvatar(item.avatar)} style={_styleSheet["pic"]} />
                      <View>
                        <View style={_styleSheet["name"]}>{item.userName}</View>
                        <View style={_styleSheet["list-info"]}>{getSimpleTime(item.creationTime)}</View>
                      </View>
                    </View>
                    <View style={[_styleSheet["r"], _styleSheet["type-demand"]]}>需求</View>
                  </View>
                  <View onClick={this.handelRouter.bind(this, '/pages/newest/demand_detail?id=' + item.id)} style={_styleSheet["title"]}>
                    {partOf(item.description, 45)}
                  </View>
                  <View style={_styleSheet["line"]}>
                    <View style={_styleSheet["list-info"]}>{item.numberOfVisits}次浏览</View>
                    <View onClick={this.delItem.bind(this, item)} style={[_styleSheet["r"], _styleSheet["type-demand"]]}>删除</View>
                  </View>

                </View>;
              break;
            }
          case 2:
            {
              tpl = <View style={_styleSheet["list-11-item"]}>
                  <View onClick={this.handelRouter.bind(this, '/pages/newest/service_detail?id=' + item.id)} style={_styleSheet["line"]}>
                    <View style={_styleSheet["l"]}>
                      <Image src={getAvatar(item.userLogoImg)} style={_styleSheet["pic"]} />
                      <View>
                        <View style={_styleSheet["name"]}>{item.userNickName}</View>
                        <View style={_styleSheet["list-info"]}>{getSimpleTime(item.creationTime)}</View>
                      </View>
                    </View>
                    <View style={[_styleSheet["r"], _styleSheet["type-services"]]}>服务</View>
                  </View>
                  <View style={_styleSheet["title"]}>
                    {partOf(item.description, 45)}
                  </View>
                  <View style={_styleSheet["line"]}>
                    <View style={_styleSheet["list-info"]}>{item.numberOfVisits}次浏览</View>
                    <View onClick={this.delItem.bind(this, item)} style={[_styleSheet["r"], _styleSheet["type-demand"]]}>删除</View>
                  </View>
                </View>;
              break;
            }
          case 3:
            {
              tpl = <View style={_styleSheet["list-12-item"]}>
                  <View onClick={this.handelRouter.bind(this, '/pages/newest/position_detail?id=' + item.id)} style={_styleSheet["line"]}>
                    <View style={_styleSheet["l"]}>
                      <Text style={_styleSheet["label"]}>{item.jobNature}</Text>
                      <Text style={_styleSheet["title"]}>{item.positionName}</Text>
                    </View>
                    <View style={[_styleSheet["r"], _styleSheet["type-position"]]}>招聘</View>
                  </View>
                  <View style={_styleSheet["price"]}>
                    {item.monthlyPay}
                  </View>
                  <View style={_styleSheet["info"]}>
                    <View style={[_styleSheet["date"], _styleSheet["list-info"]]}>
                      <Text style={[_styleSheet["op"], _styleSheet["label-text"]]}>{item.education}</Text>
                      <Text style={[_styleSheet["op"], _styleSheet["label-text"]]}>{item.recruitment}人</Text>
                      <Text style={[_styleSheet["op"], _styleSheet["label-text"]]}>{item.workYears}年</Text>
                      <Text style={[_styleSheet["op"], _styleSheet["label-text"], _styleSheet["brnone"]]}>{item.address}</Text>
                    </View>
                    <Text style={[_styleSheet["date"], _styleSheet["list-info"]]}>{item.numberOfVisits}次浏览</Text>
                    <View onClick={this.delItem.bind(this, item)} style={[_styleSheet["r"], _styleSheet["type-demand"]]}>删除</View>
                  </View>
                </View>;
              break;
            }
          case 4:
            {
              tpl = <View style={_styleSheet["list-12-item"]}>
                  <View onClick={this.handelRouter.bind(this, '/pages/newest/personnel_detail?id=' + item.id)} style={_styleSheet["line"]}>
                    <View style={_styleSheet["l"]}>
                      <Text style={_styleSheet["label"]}>{typeOfJob(item.typeOfJob)}</Text>
                      <Text style={_styleSheet["title"]}>{item.positionName}</Text>
                    </View>
                    <View style={[_styleSheet["r"], _styleSheet["type-resume"]]}>人才</View>
                  </View>
                  <View style={_styleSheet["price"]}>
                    {item.monthlyPay}
                  </View>
                  <View style={_styleSheet["info"]}>
                    <View style={[_styleSheet["date"], _styleSheet["list-info"]]}>
                      <Text style={[_styleSheet["op"], _styleSheet["label-text"]]}>{item.education}</Text>
                      <Text style={[_styleSheet["op"], _styleSheet["label-text"], _styleSheet["brnone"]]}>{item.technicalTitle}</Text>
                    </View>
                    <Text style={[_styleSheet["date"], _styleSheet["list-info"]]}>{item.numberOfVisits}次浏览</Text>
                    <View onClick={this.delItem.bind(this, item)} style={[_styleSheet["r"], _styleSheet["type-demand"]]}>删除</View>
                  </View>
                </View>;
              break;
            }
        }
        return tpl;
      })}
        {items.length <= 0 && <View style={_styleSheet["page-none-info"]}>已经没有更多信息了</View>}
      </View>;
  }
};
Personnel = tslib_1.__decorate([inject("stores"), observer], Personnel);
export default Personnel;